package system;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;

/**
 * Class Menu
 * 
 * @author Rezan Achmad
 */
public class Menu {

    public String url;
    public String text;
    public boolean visible;
    public ArrayList<Menu> items;
    public String subMenuClass = "submenu";

    public Menu(String url, String text, boolean visible, ArrayList<Menu> items) {
        this.url = url;
        this.text = text;
        this.visible = visible;
        this.items = items;
    }

    public Menu(String url, String text, boolean visible) {
        this(url, text, visible, new ArrayList<Menu>());
    }

    public String render(HttpServletRequest request) {
        StringBuilder sb = new StringBuilder();

        if(!visible) return "";
        
        // Open tag list
        sb.append("<li>");

        // Url
        sb.append("<a href=\"");
        if (url == null || "".equals(url)) {
            sb.append("#");
        } else {
            sb.append(request.getContextPath());
            sb.append(url);
        }

        // Submenu Class
        sb.append("\" class=\"");
        sb.append(subMenuClass);
        sb.append("\">");

        // Text
        sb.append(text);
        sb.append("</a>");

        // Items
        if (items.size() > 0) {
            sb.append("<ul>");
            for (int i = 0; i < items.size(); ++i) {
                sb.append(items.get(i).render(request));
            }
            sb.append("</ul>");
        }

        // Close tag list
        sb.append("</li>");

        return sb.toString();
    }
}
